package de.placidi.pressreview.textprocessing;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

/**
 * Provides some useful methods.
 * 
 * @author Alexander Placidi
 *
 */
public class MyPOSTaggerHelper {
	
	/**
	 * Reads a text from a file and does some text cleansing. 
	 * 
	 * @param file
	 * 		The source file.
	 * @param cut
	 * @param exclude
	 * @return
	 * 		Cleaned text.
	 * @throws IOException
	 */
	public static String readAndCleanText(File file, List<String> cut, List<String> exclude) throws IOException {
		FileReader fr = new FileReader(file);
		BufferedReader br = new BufferedReader(fr);
		StringBuilder sb = new StringBuilder();
		String zeile = "";

		do {
			zeile = br.readLine();

			if (zeile != null) {
				zeile = zeile.toLowerCase();

				Boolean exclusion = false;
				for (String excl : exclude) {
					if (zeile.contains(excl)) exclusion = true;
				}

				if (!exclusion) {
					String clean = cleanText(zeile, cut);
					sb.append(clean + "." + "\n");
				}

			}

		} while (zeile != null);

		br.close();

		return sb.toString();

	}

	private static String cleanText(String text, List<String> cut) {
		String cleaned = null;
		cleaned = text.replaceAll("[^A-Za-z]", " ");
		for (String c : cut) {
			cleaned = cleaned.replaceAll(c, " ");
		}

		cleaned = cleaned.trim();

		// cleaned = text.replaceAll("^[a-zA-Z]|[?]+", " ");

		return cleaned;
	}
}
